from crawler.extractors import JsonpExtractor
from crawler.fields import ContainerField, JsonField

__all__ = ['TheoutnetMainExtractor', 'TheoutnetExtractor']


class TheoutnetMainInfoExtractor(JsonpExtractor):
    container = ContainerField(json_selector='$.plp.listing.visibleProducts[0].products[*]')
    spu_id = JsonField(json_selector='$.partNumber')
    brand = JsonField(json_selector='$.designerNameEN')
    product_name = JsonField(json_selector='$.nameEN')
    cost_price = JsonField(json_selector='$.price.sellingPrice.amount')
    retail_price = JsonField(json_selector='$.price.wasPrice.amount')
    url = JsonField(json_selector='$.seo.seoURLKeyword')


class TheoutnetDetailInfoExtractor(JsonpExtractor):
    container = ContainerField(json_selector='$.pdp.detailsState.response.body.products[0].productColours[0].sKUs[*]')
    buyable = JsonField(json_selector='$.buyable')
    size = JsonField(json_selector='$.size.centralSizeLabel')
    stock = JsonField(json_selector='$.badges[0].label')
    composition = JsonField(json_selector='$.composition')
    specifications = JsonField(json_selector='$.size.schemas[*]', many=True)


class TheoutnetColorInfoExtractor(JsonpExtractor):
    color = JsonField(json_selector='$.pdp.detailsState.response.body.products[0].productColours[0].label')
    product_des = JsonField(
        json_selector='$.pdp.detailsState.response.body.products[0].productColours[0].technicalDescription')
    imageViews = JsonField(json_selector='$.pdp.detailsState.response.body.products[0].productColours[0].imageViews')


# class TheoutnetSpecificationsInfoExtractor(JsonpExtractor):
#         container=JsonField(json_selector='$.pdp.detailsState.response.body.products[0].productColours[0].sKUs[0].size.schemas[*]')

class TheoutnetMainExtractor:
    @classmethod
    def get_item(cls, response, resp):
        return cls._extract(response, resp)

    @classmethod
    def _extract(cls, response, resp):
        task = response.meta['task']
        sex = task['sex']
        category = task['category']

        info_list = list()
        for item in TheoutnetMainInfoExtractor.get_items(response, resp):
            info_dict = dict()
            info_dict['spu_id'] = item.spu_id
            # 品牌
            info_dict['brand'] = item.brand
            # 品类
            info_dict['title'] = item.product_name
            info_dict['category'] = category
            # 性别
            info_dict['sex'] = sex
            # 图集 需要请求详情页
            info_dict['atlas'] = ''
            # 成本价 原价对应公价
            info_dict['cost_price'] = item.cost_price / 100
            # 公价
            info_dict['retail_price'] = item.retail_price / 100
            info_dict['url'] = 'https://www.theoutnet.com/en-hk/shop/product' + item.url
            # 规格 ，
            # info_dict['specifications'] = ''
            # y颜色
            info_dict['color'] = ''
            # 货币单位
            info_dict['currency'] = 'HKD'
            # 成分
            info_dict['composition'] = ''
            # 商品描述。详情页的
            info_dict['product_des'] = ''
            info_dict['size_stock'] = ''
            info_list.append(info_dict)

        return info_list


stock_dict = {
    'ONE': 1,
    'TWO': 2,
    'THREE': 3,
    'FOUR': 4,
    'FIVE': 5,
    'SIX': 6,
}


class TheoutnetExtractor:
    @classmethod
    def get_item(cls, response, resp):
        return cls._extract(response, resp)

    @classmethod
    def _extract(cls, response, resp):
        size_stock_list = list()
        # spec_list = list()
        composition = ''
        for item in TheoutnetDetailInfoExtractor.get_items(response, resp):
            info_dict = dict()
            spec_dict = dict()
            info_dict['size'] = item.size

            info_dict['stock'] = item.stock if item.stock else 10
            if info_dict['stock'] == 0 or 'SOLD OUT' in str(info_dict['stock']):
                continue
            if 'LEFT' in str(info_dict['stock']):
                info_dict['stock'] = stock_dict[info_dict['stock'].split(' ')[1].strip()]

            spec_dict['labels'] = [item.size]
            spec_dict['name'] = 'OUTNET'

            # spec_info = item.specifications
            # spec_info.append(spec_dict)

            # spec_list.append(spec_info)
            # 只要一个
            composition = item.composition
            size_stock_list.append(info_dict)

        item = TheoutnetColorInfoExtractor.get_item(response, resp)
        color = item.color
        product_des = item.product_des
        image_list = item.imageViews
        return color, product_des, image_list, composition, size_stock_list
